package com.ibl.rover.lite.client.Activity;

import java.util.ArrayList;
import java.util.List;

import com.ibl.rover.lite.client.Logger;
import com.ibl.rover.lite.client.R;
import com.ibl.rover.lite.client.Application.ApplicationContext;
import com.ibl.rover.lite.client.Application.OrderBookingLine;
import com.ibl.rover.lite.client.constants.RoverConstants;
import com.ibl.rover.lite.common.bean.AppSetupBean;
import com.ibl.rover.lite.common.bean.CustomerBean;
import com.ibl.rover.lite.common.bean.OrderBean;
import com.ibl.rover.lite.common.bean.ProductBean;
import com.ibl.rover.lite.common.bean.ProductOrderBean;
import com.ibl.rover.lite.common.db.Criteria;
import com.ibl.rover.lite.common.db.DatabaseManager;
import com.ibl.rover.lite.common.db.Criteria.Operators;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

public class OrderSummaryActivity extends Activity implements OnClickListener {

	private Logger logger = new Logger();
	private ApplicationContext roverApp;
	private DatabaseManager manager = null;
	private CustomerBean customerBean;
	private OrderBean orderBean;
	private Spinner salesTypeSpinner;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sales_order_detail_report);
		initialize();
	}

	/**
	 * Initialize Resources
	 */
	private void initialize() {
		try {
			roverApp = (ApplicationContext) getApplication();
			manager = roverApp.getDatabaseManager();

			customerBean = (CustomerBean) roverApp.getViewContext().get(
					RoverConstants.CURRENT_CUSTOMER);

			orderBean = (OrderBean) roverApp.getViewContext().get(
					RoverConstants.CURRENT_PREVIOUS_ORDER_SUCESS);

			((TextView) findViewById(R.id.txt_date)).setText(orderBean
					.getDate());

			((TextView) findViewById(R.id.txt_order_no)).setText(String
					.valueOf(orderBean.getOrderId()));

			((TextView) findViewById(R.id.txt_customer_name))
					.setText(customerBean.getName());

			((TextView) findViewById(R.id.txt_customer_code))
					.setText(customerBean.getCode());

			((TextView) findViewById(R.id.txt_discount)).setText("0.0");

			((TextView) findViewById(R.id.txt_tax)).setText("0.0");

			((TextView) findViewById(R.id.txt_gross_amount)).setText(orderBean
					.getAmount());

			((TextView) findViewById(R.id.txt_net_amount)).setText(orderBean
					.getAmount());
			
			((TextView) findViewById(R.id.txt_sales_type)).setText(orderBean
					.getSalesType());

			// ((TextView) findViewById(R.id.txt_tso_code_value))
			// .setText(orderBookingLine.getOrderAmount());

//			salesTypeSpinner = (Spinner) findViewById(R.id.sales_type_spinner);
//
//			ArrayAdapter<CharSequence> routeAdapter = ArrayAdapter
//					.createFromResource(this, R.array.slaes_type_filter,
//							android.R.layout.simple_spinner_item);
//
//			routeAdapter
//					.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//			salesTypeSpinner.setAdapter(routeAdapter);

			((Button) findViewById(R.id.order_detail_report_cancel_btn))
					.setOnClickListener(this);
			((Button) findViewById(R.id.order_detail_report_edit_btn))
					.setOnClickListener(this);

		} catch (Exception e) {

			logger.logException("Initialize Exception :", e);
		}
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.order_detail_report_edit_btn:
			editOrder();
			break;
		case R.id.order_detail_report_cancel_btn: {
			roverApp.getViewContext().clear();
			this.finish();
		}
			break;

		default:
			break;
		}
	}

	private void editOrder() {
		try {
			getOrderDetail();
		} catch (Exception e) {
			logger.logException("Edit order ", e);
		}
	}

	/**
	 * Get Order DEtial
	 */
	private void getOrderDetail() {
		try {

			Criteria<CustomerBean> criteria = manager
					.generateCriteria(CustomerBean.class);
			criteria.addRestriction(CustomerBean.COL_CODE, Operators.isEqual,
					orderBean.getCustomerCode());

			List<CustomerBean> list = criteria.execute();
			if (list.size() > 0) {
				roverApp.getViewContext().put(RoverConstants.CURRENT_CUSTOMER,
						list.get(0));
				roverApp.getViewContext()
						.put(RoverConstants.CURRENT_PREVIOUS_ORDER_SUCESS,
								orderBean);
				roverApp.getViewContext()
						.put(RoverConstants.CURRENT_PREVIOUS_ORDER_SUCESS_BOOKIG_LINE,
								getProductBeanList(orderBean));
				Intent intent = new Intent(this, SalesBookingActivity.class);
				startActivity(intent);

			}

		} catch (Exception e) {
			logger.logException("get Order Detail", e);
		}

	}

	private OrderBookingLine getProductBeanList(OrderBean orderBean) {
		try {
			Criteria<ProductOrderBean> criteria = manager
					.generateCriteria(ProductOrderBean.class);
			criteria.addRestriction(ProductOrderBean.COL_ORDER_KEY,
					Operators.isEqual, orderBean.getOrderId());

			List<ProductOrderBean> list = criteria.execute();
			List<ProductBean> productBeans = new ArrayList<ProductBean>();
			List<String> quantityList = new ArrayList<String>();
			ProductOrderBean productOrderBean;
			if (list.size() > 0) {

//				Criteria<ProductBean> criteria2 = manager
//						.generateCriteria(ProductBean.class);
				productOrderBean = list.get(0);
//				criteria2.addRestriction(ProductBean.COL_PRODUCT_CODE,
//						Operators.isEqual, productOrderBean.getProductCode());
				quantityList.add(productOrderBean.getQuantity());
				productBeans.add(getProductCode(productOrderBean.getProductCode()));
				for (int i = 1; i < list.size(); i++) {

					productOrderBean = list.get(i);
//					criteria2.addRestriction(ProductBean.COL_PRODUCT_CODE,
//							Operators.isEqual,
//							productOrderBean.getProductCode());
					quantityList.add(productOrderBean.getQuantity());
					productBeans.add(getProductCode(productOrderBean.getProductCode()));
				}
//				productBeans = criteria2.execute();
				OrderBookingLine orderBookingLine = new OrderBookingLine();
				orderBookingLine.setOrderAmount(orderBean.getAmount());
				orderBookingLine.setProductBeanList(productBeans);
				orderBookingLine.setProductQuantitylist(quantityList);
				return orderBookingLine;
			}

		} catch (Exception e) {
			logger.logException("getproductBeanList", e);
		}
		return null;
	}

	private ProductBean getProductCode(String productCode) {
		try {
			Criteria<ProductBean> criteria2 = manager
					.generateCriteria(ProductBean.class);
			criteria2.addRestriction(ProductBean.COL_PRODUCT_CODE,
					Operators.isEqual, productCode);
			List<ProductBean> list = criteria2.execute();
			if (list.size() > 0)
				return list.get(0);

		} catch (Exception e) {
			logger.logException("getProductCode", e);
		}

		return null;
	}
}
